package com.yaojiaxun.logisticsPlatformSystem.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.yaojiaxun.logisticsPlatformSystem.dto.OrderInfoDTO;
import com.yaojiaxun.logisticsPlatformSystem.dto.PageDTO;
import com.yaojiaxun.logisticsPlatformSystem.entity.OrderInfo;
import com.yaojiaxun.logisticsPlatformSystem.mapper.OrderInfoMapper;
import com.yaojiaxun.logisticsPlatformSystem.util.SnowFlakeIDWorker;
import com.yaojiaxun.logisticsPlatformSystem.util.TimeFormat;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: yjx_j
 * @Date: 2019/2/9 19:28
 * @Description:
 */
@Service
public class OrderInfoService {
    @Autowired
    private OrderInfoMapper orderInfoMapper;

    public OrderInfo findOrderByOrderId(Long orderId){
        return orderInfoMapper.selectOrderById(orderId);
    }

    public OrderInfoDTO beanToDTO(OrderInfo orderInfo){
        if (orderInfo == null)
            return null;
        OrderInfoDTO orderInfoDTO = new OrderInfoDTO();
        BeanUtils.copyProperties(orderInfo, orderInfoDTO);
        orderInfoDTO.setCreateTime(TimeFormat.convertTime(orderInfo.getCreateTime()));
        orderInfoDTO.setUpdateTime(TimeFormat.convertTime2(orderInfo.getUpdateTime()));
        return orderInfoDTO;
    }

    public PageDTO<OrderInfoDTO> findPage(Integer pageNum){
        PageDTO<OrderInfoDTO> pageDTO = new PageDTO<>();
        Page<OrderInfoDTO> page = new Page<>(pageNum,10);
        List<OrderInfoDTO> results = orderInfoMapper.selectPage(page, null).stream().map(this::beanToDTO).collect(Collectors.toList());
        pageDTO.setResult(results);
        pageDTO.setPage(page);
        return pageDTO;
    }

    public void insertOrderInfo(OrderInfo orderInfo){
        orderInfo.setOrderId(new SnowFlakeIDWorker(7 ,1).nextId());
        orderInfo.setCreateTime(Instant.now().toEpochMilli());
        orderInfo.setUpdateTime(Instant.now().toEpochMilli());
        orderInfo.setOrderStatus("未接单");
        orderInfoMapper.insertOrderInfo(orderInfo);
    }

    public void deleteOrderInfo(Long orderId){
        orderInfoMapper.deleteOrderById(orderId);
    }

    public Boolean updateOrderInfo(OrderInfo orderInfo){
        orderInfo.setUpdateTime(Instant.now().toEpochMilli());
        return orderInfoMapper.updateOrderInfoById(orderInfo)>0;
    }

    public List<OrderInfoDTO> findOrdersByOrderCom(String orderCom){
        List<OrderInfo> list = orderInfoMapper.findOrdersByComName(orderCom);
        List<OrderInfoDTO> results = new ArrayList<>();
        for (OrderInfo order : list) {
            OrderInfoDTO orderInfoDTO = beanToDTO(order);
            results.add(orderInfoDTO);
        }
        return results;
    }

    //找出某个客户的已完成订单
    public List<OrderInfoDTO> findFinishedOrdersByClientAccount(String clientAccount){
        List<OrderInfo> list = orderInfoMapper.findFinishedOrdersByClientAccount(clientAccount);
        List<OrderInfoDTO> results = new ArrayList<>();
        for (OrderInfo orderInfo : list){
            OrderInfoDTO orderInfoDTO = beanToDTO(orderInfo);
            results.add(orderInfoDTO);
        }
        return results;
    }
    //找出某个客户的未接单订单
    public List<OrderInfoDTO> findUnfinishedOrdersByClientAccount(String clientAccount){
        List<OrderInfo> list = orderInfoMapper.findUnfinishedOrdersByClientAccount(clientAccount);
        List<OrderInfoDTO> results = new ArrayList<>();
        for (OrderInfo orderInfo : list){
            OrderInfoDTO orderInfoDTO = beanToDTO(orderInfo);
            results.add(orderInfoDTO);
        }
        return results;
    }
    //找出某个客户的问题订单(被拒绝)
    public List<OrderInfoDTO> findRefusedOrdersByClientAccount(String clientAccount){
        List<OrderInfo> list = orderInfoMapper.findRefusedOrdersByClientAccount(clientAccount);
        List<OrderInfoDTO> results = new ArrayList<>();
        for (OrderInfo orderInfo : list){
            OrderInfoDTO orderInfoDTO = beanToDTO(orderInfo);
            results.add(orderInfoDTO);
        }
        return results;
    }
    //找出某个客户的已接单订单
    public List<OrderInfoDTO> findReceivedOrdersByClientAccount(String clientAccount){
        List<OrderInfo> list = orderInfoMapper.findReceivedOrdersByClientAccount(clientAccount);
        List<OrderInfoDTO> results = new ArrayList<>();
        for (OrderInfo orderInfo : list){
            OrderInfoDTO orderInfoDTO = beanToDTO(orderInfo);
            results.add(orderInfoDTO);
        }
        return results;
    }
}
